package com.example.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class   MainActivity extends Activity implements View.OnClickListener {
    private SQLiteHelper helper;
    private Cursor cursor;
    private ListView lvUsers;
    private EditText editName;
    private EditText editPhone;
    private EditText editAdress;

    private int id=0;

    private ImageButton b1,b2,b3,b4;



    private void bindViews() {

        b1 = findViewById(R.id.b1);
        b2 = findViewById(R.id.b2);
        b3 = findViewById(R.id.b3);
        b4 = findViewById(R.id.b4);

        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        b3.setOnClickListener(this);
        b4.setOnClickListener(this);
    }
    @Override
    public void onClick(View v){
        switch (v.getId())
        {
            case R.id.b1:
                this.addRec();
                break;
            case R.id.b2:
                this.deleteRec();
                break;
            case R.id.b3:
                this.queryRec();
                break;
            case R.id.b4:
                this.editRec();
                break;
        }

    }

    @Override
    public void onCreate (Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bindViews();

        lvUsers = this.findViewById(R.id.listview);
        editName =  this.findViewById(R.id.et1);
        editPhone =this.findViewById(R.id.et2);
        editAdress = this.findViewById(R.id.et3);

        //表中内容填充到自定义ListView
        helper = new SQLiteHelper(this);
        cursor = helper.select();
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                this,
                R.layout.list,
                cursor,
                new String[]{"Name", "PhoneNum", "Adress"},
                new int[]{R.id.name, R.id.phone, R.id.adress}
        );
        lvUsers.setAdapter(adapter);


        // 设置OnItemClickListener监听事件
        lvUsers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                cursor.moveToPosition(arg2);            // 将cursor移到所点击的值
                id  = cursor.getInt(0);                // 取得字段_id的值
                editName.setText(cursor.getString(1));    // 取得字段Rec_text的值
                editPhone.setText(cursor.getString(2));
                editAdress.setText(cursor.getString(3));
                Bundle bd = new Bundle();
                bd.putString("name",cursor.getString(1));
                bd.putString("number",cursor.getString(2));
                Intent intent=new Intent(MainActivity.this,Contact.class);
                intent.putExtras(bd);
                startActivity(intent);
//                    Uri uri=Uri.parse("tel:"+editPhone.getText());
//                    Intent intent=new Intent(Intent.ACTION_DIAL,uri);
//                    startActivity(intent);
            }
        });

    }

    //添加记录
    private void addRec ()
    {
        if (editName.getText().toString().equals(""))
            return;
        helper.insert(editName.getText().toString(), editPhone.getText().toString(), editAdress.getText().toString());
        //重新加载数据
        cursor.requery();
        lvUsers.invalidateViews();
        editName.setText("");
        editPhone.setText("");
        editAdress.setText("");
    }

    // 修改记录
    private void editRec ()
    {
        if (editName.getText().toString().equals(""))
            return;
        helper.update(id, editName.getText().toString(), editPhone.getText().toString(), editAdress.getText().toString());

        //重新加载数据
        cursor.requery();
        lvUsers.invalidateViews();
        editName.setText("");
        editPhone.setText("");
        editAdress.setText("");
    }

    //查询
    private void queryRec()
    {
        String et = editName.getText().toString();
        String args[] = new String[]{"%" + et + "%"};
        cursor = helper.query(args);
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                this,
                R.layout.list,
                cursor,
                new String[]{"Name", "PhoneNum", "Adress"},
                new int[]{R.id.name, R.id.phone, R.id.adress}
        );
        lvUsers.setAdapter(adapter);
    }

    //删除记录
    private void deleteRec ()
    {
        helper.delete(id);
        cursor.requery();
        lvUsers.invalidateViews();
        editName.setText("");
    }

}
